/**
 * Created by meizhao on 2017/1/3.
 */

(function (window) {
    console.log('common.window ready...');

    var ZCKJ = ZCKJ ? ZCKJ : {};
    //获取当前窗口宽度
    ZCKJ.getWindowWidth = function () {
        var winWidth = 0;
        if (window.innerWidth)
            winWidth = window.innerWidth;
        else if ((document.body) && (document.body.clientWidth))
            winWidth = document.body.clientWidth;
        return winWidth;
    }
    //判断是否为对象
    ZCKJ.isEmptyObject = function (obj) {
        if (JSON.stringify(obj) === '{}') {
            return true;
        }
        return false;
    }
    //判断是否为空
    ZCKJ.isEmpty = function (obj) {
        var types = typeof obj;
        if(types == 'undefined' || obj == '' || obj == null) {
            return true;
        }
        return false;
    }
    //几位,值，最小，最大
    ZCKJ.isLength = function (v,min,max) {
        var tel = new RegExp("^.{" + min + "," + max + "}$");
        return (tel.test(v));
    }
    //几位纯数字
    ZCKJ.isNum = function (v,min,max) {
        var tel = new RegExp("^\\d{" + min + "," + max + "}$");
        return (tel.test(v));
    }

    //判断是否Email
    ZCKJ.isEmail = function (v) {
        var tel = new RegExp("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");
        return (tel.test(v));
    }
    //判断是否电话
    ZCKJ.isTel = function (v) {
        var tel = new RegExp("^[[0-9]{3}-|\[0-9]{4}-]?(\[0-9]{8}|[0-9]{7})?$");
        return (tel.test(v));
    }
    ZCKJ.isPhone = function (v) {
        var tel = new RegExp("^[1][0-9]{10}$");
        return (tel.test(v));
    }
    //判断是否移动设备
    ZCKJ.isMobile = function () {
        return !!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i));

    }
    //是否苹果系统
    ZCKJ.isIos = function () {
        return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);

    }
    //是否微信
    ZCKJ.isWeixin = function () {
        return navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger";
    }
    //设置Cookie
    ZCKJ.setCookie = function (c_name, value, expiredays) {
        if (ZCKJ.isEmptyObject(c_name) || ZCKJ.isEmptyObject(value))return;
        var currentDate = new Date();
        currentDate.setDate(currentDate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + currentDate.toGMTString());
    }
    //获取Cookie
    ZCKJ.getCookie = function (CookieName) {
        if (document.cookie.length > 0) {
            CookieNameStart = document.cookie.indexOf(CookieName + "=");
            if (CookieNameStart != -1) {
                CookieNameStart = CookieNameStart + CookieName.length + 1;
                c_end = document.cookie.indexOf(";", CookieNameStart);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(CookieNameStart, c_end))
            }
        }
        return ""
    }
    //设置
    ZCKJ.localStorage = function(key,value){
        if(window.localStorage){
            //读取
            if(ZCKJ.isEmpty(value)){
                return window.localStorage.getItem(key);
            }else {
                window.localStorage.setItem(key,value);
                return true;
            }
        }
        return null;
    }
    //获取人民币大写
    ZCKJ.getAmountToZhCN = function (n) {
        if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
            return "数据非法";
        var unit = "仟佰拾亿仟佰拾万仟佰拾元角分", str = "";
        n += "00";
        var p = n.indexOf('.');
        if (p >= 0)
            n = n.substring(0, p) + n.substr(p + 1, 2);

        unit = unit.substr(unit.length - n.length);
        for (var i = 0; i < n.length; i++)
            str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);

        return str.replace(/零(仟|佰|拾|角)/g, "零")
            .replace(/(零)+/g, "零")
            .replace(/零(万|亿|元)/g, "$1")
            .replace(/(亿)万|壹(拾)/g, "$1$2")
            .replace(/^元零?|零分/g, "")
            .replace(/元$/g, "元整");
    }
    //打开弹出窗口
    ZCKJ.openWindow = function (url, innerWidths, innerHeights) {
        if (ZCKJ.isEmptyObject(innerWidths)) innerWidths = 850;
        if (ZCKJ.isEmptyObject(innerHeights)) innerHeights == 650;
        var ScreenWidth = screen.availWidth
        var ScreenHeight = screen.availHeight
        var StartX = (ScreenWidth - innerWidths) / 2
        var StartY = (ScreenHeight - innerHeights) / 2
        window.open(url, '详情', 'height=' + innerHeights + ',width=' + innerWidths + ',top=' + StartY + ',left=' + StartX + ',toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no');
    }
    ZCKJ.post = function (url, data, success, error, load,timeout) {
        var loadId = -1;
        if ($.isEmptyObject(load)) {
            if(typeof layer.load == 'function'){
                loadId = layer.load();
            }else{
                loadId = ZCKJ.loading();
            }

        }

        if($.isEmptyObject(timeout)){
            timeout =0;
        }
        $.ajax({
            type: "POST",
            cache: false,
            url: url,
            data: data,
            timeout :timeout,
            success: function (data) {
                if (typeof success == 'function') {
                    success(data);
                }
            },
            error: function (data) {
                var msg = "status: " + data.status + "; statusText: " + data.statusText;
                if (typeof success == 'function') {
                    error(msg);
                    return;
                }
                alert('网络出错：'.msg);

            },
            complete: function () {
                if (loadId > -1) {
                    layer.close(loadId);
                }
            }
        })
    }
    /*****layer弹层********/
    ZCKJ.msg = function (msg,time,fun) {
        if(ZCKJ.isEmpty(time))time = 3;
        layer.open({
            type:0,
            content:msg,
            time:time,
            end:function (index) {
                fun && fun(index);
            }
        });
    }

    ZCKJ.alert = function (msg,fun) {
        layer.open({
            type:0,
            title:"温馨提示",
            content:msg,
            btn: '确 定',
            shadeClose:false,
            end:function (index) {
                fun && fun(index);
            }
        });
    }
    ZCKJ.msg1 = function (msg,time,fun) {
        if(ZCKJ.isEmpty(time))time = 3;
        layer.open({
            type:0,
            content:msg,
            skin: 'msg',
            time: time,
            end:function (index) {
                fun && fun(index);
            }
        });
    }
    ZCKJ.confirm = function (msg,btn,fun,onfun) {
        //['刷新', '不要']
        layer.open({
            content: msg
            ,btn: btn
            ,yes: function(index){
                layer.close(index);
                fun && fun(index);

            }
            ,no:function (index) {
                layer.close(index);
                onfun && onfun(index);
            }
        });
    }
    ZCKJ.footerMsg = function (msg,fun) {
        //底部提示
        layer.open({
            content: msg
            ,skin: 'footer'
            ,end:function (index) {
                fun && fun(index);
            }
        });
    }
    ZCKJ.footerAlert = function (msg,btn,fun) {
        //底部提示
        layer.open({
            content: msg
            ,skin: 'footer'
            ,btn:btn
            ,yes:function (index) {
                layer.close(index);
                fun && fun(index);
            }
        });
    }
    ZCKJ.loading = function (msg,shadeClose,type) {
        if(typeof  shadeClose == 'undefined')shadeClose = false;
        if(typeof  type == 'undefined')type = 1;
        if(ZCKJ.isEmpty(msg))msg = '';
        var index = layer.open({
            type: type
            ,shade:false
            ,content: msg
            ,shadeClose : shadeClose
        });
        return index;
    }
    window.ZCKJ = ZCKJ;

})(window);